import 'package:flutter/material.dart';
import 'function.dart';
import 'package:flutter/cupertino.dart';

var decimal = 3;

class TopData {
  String date = "全网统计";
  int wkb_num = 0;
  int block_num = 0;
  String average_onlinetime = "0";
  int average_bandwidth = 0;
  int average_disk = 0;

  List topN_wkb = [];
  List topN_bandwidth = [];
  List topN_disk = [];
  fromMap(data) {
    date = data['date'];
    wkb_num = data['wkb_num'];
    block_num = data['block_num'];
    average_onlinetime = data['average_onlinetime'];
    average_bandwidth = data['average_bandwidth'];
    average_disk = data['average_disk'];
    topN_wkb = data['topN_wkb'];
    topN_bandwidth = data['topN_bandwidth'];
    topN_disk = data['topN_disk'];
  }
}

class TopPage extends StatefulWidget {
  TopPage(this.mainColor);
  final Color mainColor;
  @override
  _TopPageState createState() => _TopPageState();
}

class _TopPageState extends State<TopPage> {
  TopData topData = new TopData();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initTop();
  }

  initTop() async {
    var res = await getWankeTop();
    if (res == null || res['iRet'] != 0) {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text('错误信息'),
            content: new Text("获取全网统计信息失败，请重试"),
            actions: <Widget>[
              FlatButton(
                child: Text('重试'),
                onPressed: () {
                  initTop();
                  Navigator.of(context).pop();
                },
              ),
              FlatButton(
                child: Text('返回'),
                onPressed: () {
                  Navigator.of(context).pop();
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    } else {
      topData.fromMap(res['data']);
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: new Color(0xFFF5F5F5),
      appBar: new CupertinoNavigationBar(
        actionsForegroundColor: Colors.white,
        backgroundColor: widget.mainColor,
        border: new Border(bottom: BorderSide.none),
        middle: new Text(
          topData.date,
          style: new TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.normal,
          ),
        ),
      ),
      body: new Container(
        padding: new EdgeInsets.only(left: 10.0, right: 10.0),
        child: new ListView(
          children: <Widget>[
            new SizedBox(
              height: 10.0,
            ),
            new Card(
              elevation: 4.0,
              child: new Stack(
                children: <Widget>[
                  new Container(
                    padding: new EdgeInsets.only(top: 20.0, bottom: 20.0),
                    decoration: new BoxDecoration(
                      gradient: LinearGradient(colors: [new Color(0xFF00D0FF), new Color(0xFF608DDE)], begin: Alignment.topLeft, end: Alignment.bottomRight),
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                    ),
                    child: new Column(
                      children: <Widget>[
                        new Text(
                          "链克区块链累计情况",
                          style: new TextStyle(color: Colors.white, fontSize: 20.0),
                        ),
                        new SizedBox(
                          height: 20.0,
                        ),
                        new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: <Widget>[
                            new Column(
                              children: <Widget>[
                                new Text(
                                  topData.wkb_num.toString(),
                                  style: new TextStyle(color: Colors.white, fontSize: 32.0),
                                ),
                                new Text(
                                  "链克产量",
                                  style: new TextStyle(color: Colors.white, fontSize: 16.0),
                                ),
                              ],
                            ),
                            new Column(
                              children: <Widget>[
                                new Text(
                                  topData.block_num.toString(),
                                  style: new TextStyle(color: Colors.white, fontSize: 32.0),
                                ),
                                new Text(
                                  "区块高度",
                                  style: new TextStyle(color: Colors.white, fontSize: 16.0),
                                ),
                              ],
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                  new Image.asset(
                    "images/topicon2.png",
                    width: 60.0,
                  ),
                ],
              ),
            ),
            new SizedBox(
              height: 10.0,
            ),
            new Card(
              elevation: 4.0,
              child: new Stack(
                children: <Widget>[
                  new Container(
                    padding: new EdgeInsets.only(top: 20.0, bottom: 20.0),
                    decoration: new BoxDecoration(
                      gradient: LinearGradient(colors: [new Color(0xFFFF76F4), new Color(0xFF7977FF)], begin: Alignment.topLeft, end: Alignment.bottomRight),
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                    ),
                    child: new Column(
                      children: <Widget>[
                        new Text(
                          "资源共享人均表现",
                          style: new TextStyle(color: Colors.white, fontSize: 20.0),
                        ),
                        new SizedBox(
                          height: 20.0,
                        ),
                        new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: <Widget>[
                            new Column(
                              children: <Widget>[
                                new Row(
                                  crossAxisAlignment: CrossAxisAlignment.baseline,
                                  textBaseline: TextBaseline.alphabetic,
                                  children: <Widget>[
                                    new Text(
                                      topData.average_onlinetime,
                                      style: new TextStyle(color: Colors.white, fontSize: 32.0),
                                    ),
                                    new Text(
                                      "小时",
                                      style: new TextStyle(color: Colors.white, fontSize: 12.0, fontStyle: FontStyle.italic),
                                    ),
                                  ],
                                ),
                                new Text(
                                  "在线时长",
                                  style: new TextStyle(color: Colors.white, fontSize: 16.0),
                                ),
                              ],
                            ),
                            new Column(
                              children: <Widget>[
                                new Row(
                                  crossAxisAlignment: CrossAxisAlignment.baseline,
                                  textBaseline: TextBaseline.alphabetic,
                                  children: <Widget>[
                                    new Text(
                                      topData.average_bandwidth.toString(),
                                      style: new TextStyle(color: Colors.white, fontSize: 32.0),
                                    ),
                                    new Text(
                                      "Mbps",
                                      style: new TextStyle(color: Colors.white, fontSize: 12.0, fontStyle: FontStyle.italic),
                                    ),
                                  ],
                                ),
                                new Text(
                                  "上行带宽",
                                  style: new TextStyle(color: Colors.white, fontSize: 16.0),
                                ),
                              ],
                            ),
                            new Column(
                              children: <Widget>[
                                new Row(
                                  crossAxisAlignment: CrossAxisAlignment.baseline,
                                  textBaseline: TextBaseline.alphabetic,
                                  children: <Widget>[
                                    new Text(
                                      topData.average_disk.toString(),
                                      style: new TextStyle(color: Colors.white, fontSize: 32.0),
                                    ),
                                    new Text(
                                      "GB",
                                      style: new TextStyle(color: Colors.white, fontSize: 12.0, fontStyle: FontStyle.italic),
                                    ),
                                  ],
                                ),
                                new Text(
                                  "贡献存储",
                                  style: new TextStyle(color: Colors.white, fontSize: 16.0),
                                ),
                              ],
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                  new Image.asset(
                    "images/topicon1.png",
                    width: 60.0,
                  ),
                ],
              ),
            ),
            new SizedBox(
              height: 10.0,
            ),
            new Stack(
              children: <Widget>[
                new Padding(
                  padding: EdgeInsets.only(top: 12.0),
                  child: new Card(
                    elevation: 4.0,
                    child: new Column(
                      children: <Widget>[
                        new Padding(
                          padding: EdgeInsets.only(top: 20.0, bottom: 10.0),
                          child: new Text(
                            "链克榜",
                            style: new TextStyle(
                              fontSize: 22.0,
                              color: new Color(0xFF333333),
                            ),
                          ),
                        ),
                        new Container(
                          height: 170.0,
                          width: MediaQuery.of(context).size.width - 20,
                          child: new Column(
                            children: topData.topN_wkb.map((wkb) {
                              return new Padding(
                                padding: EdgeInsets.only(top: 5.0, bottom: 5.0, left: 12.0, right: 12.0),
                                child: new Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    new Row(
                                      children: <Widget>[
                                        topData.topN_wkb.indexOf(wkb) == 0
                                            ? new Image.asset(
                                          "images/top1.png",
                                          width: 16.0,
                                        )
                                            : topData.topN_wkb.indexOf(wkb) == 1
                                            ? new Image.asset(
                                          "images/top2.png",
                                          width: 16.0,
                                        )
                                            : topData.topN_wkb.indexOf(wkb) == 2
                                            ? new Image.asset(
                                          "images/top3.png",
                                          width: 16.0,
                                        )
                                            : new SizedBox(
                                          width: 16.0,
                                        ),
                                        new SizedBox(
                                          width: 10.0,
                                        ),
                                        new Text(
                                          wkb['phone'] != "" ? wkb['phone'] : "邮箱用户",
                                          style: new TextStyle(
                                            fontSize: 16.0,
                                            color: new Color(0xFF666666),
                                          ),
                                        ),
                                      ],
                                    ),
                                    new Text(
                                      wkb['wkb'].toString() + "个",
                                      style: new TextStyle(
                                        fontSize: 16.0,
                                        color: new Color(0xFF666666),
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }).toList(),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                new Padding(
                  padding: new EdgeInsets.only(left: 10.0),
                  child: new Image.asset(
                    "images/toplist1.png",
                    width: 50.0,
                  ),
                ),
              ],
            ),
            new SizedBox(
              height: 10.0,
            ),
            new Stack(
              children: <Widget>[
                new Padding(
                  padding: EdgeInsets.only(top: 12.0),
                  child: new Card(
                    elevation: 4.0,
                    child: new Column(
                      children: <Widget>[
                        new Padding(
                          padding: EdgeInsets.only(top: 20.0, bottom: 10.0),
                          child: new Text(
                            "贡献上行带宽榜",
                            style: new TextStyle(
                              fontSize: 22.0,
                              color: new Color(0xFF333333),
                            ),
                          ),
                        ),
                        new Container(
                          height: 170.0,
                          width: MediaQuery.of(context).size.width - 20,
                          child: new Column(
                            children: topData.topN_bandwidth.map((bandwidth) {
                              return new Padding(
                                padding: EdgeInsets.only(top: 5.0, bottom: 5.0, left: 12.0, right: 12.0),
                                child: new Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    new Expanded(
                                      flex: 2,
                                      child: new Row(
                                        children: <Widget>[
                                          topData.topN_bandwidth.indexOf(bandwidth) == 0
                                              ? new Image.asset(
                                            "images/top1.png",
                                            width: 16.0,
                                          )
                                              : topData.topN_bandwidth.indexOf(bandwidth) == 1
                                              ? new Image.asset(
                                            "images/top2.png",
                                            width: 16.0,
                                          )
                                              : topData.topN_bandwidth.indexOf(bandwidth) == 2
                                              ? new Image.asset(
                                            "images/top3.png",
                                            width: 16.0,
                                          )
                                              : new SizedBox(
                                            width: 16.0,
                                          ),
                                          new SizedBox(
                                            width: 10.0,
                                          ),
                                          new Text(
                                            bandwidth['phone'] != "" ? bandwidth['phone'] : "邮箱用户",
                                            style: new TextStyle(
                                              fontSize: 16.0,
                                              color: new Color(0xFF666666),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    new Expanded(
                                      child: new Text(
                                        bandwidth['bandwidth'].toString() + " GB",
                                        style: new TextStyle(
                                          fontSize: 16.0,
                                          color: new Color(0xFF666666),
                                        ),
                                      ),
                                    ),
                                    new Expanded(
                                      child: new Text(
                                        bandwidth['wkb'].toStringAsFixed(decimal) + "个",
                                        style: new TextStyle(
                                          fontSize: 16.0,
                                          color: new Color(0xFF666666),
                                        ),
                                        textAlign: TextAlign.right,
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }).toList(),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                new Padding(
                  padding: new EdgeInsets.only(left: 10.0),
                  child: new Image.asset(
                    "images/toplist2.png",
                    width: 50.0,
                  ),
                ),
              ],
            ),
            new SizedBox(
              height: 10.0,
            ),
            new Stack(
              children: <Widget>[
                new Padding(
                  padding: EdgeInsets.only(top: 12.0),
                  child: new Card(
                    elevation: 4.0,
                    child: new Column(
                      children: <Widget>[
                        new Padding(
                          padding: EdgeInsets.only(top: 20.0, bottom: 10.0),
                          child: new Text(
                            "贡献存储榜",
                            style: new TextStyle(
                              fontSize: 22.0,
                              color: new Color(0xFF333333),
                            ),
                          ),
                        ),
                        new Container(
                          height: 170.0,
                          width: MediaQuery.of(context).size.width - 20,
                          child: new Column(
                            children: topData.topN_disk.map((disk) {
                              return new Padding(
                                padding: EdgeInsets.only(top: 5.0, bottom: 5.0, left: 12.0, right: 12.0),
                                child: new Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    new Expanded(
                                      flex: 4,
                                      child: new Row(
                                        children: <Widget>[
                                          topData.topN_disk.indexOf(disk) == 0
                                              ? new Image.asset(
                                            "images/top1.png",
                                            width: 16.0,
                                          )
                                              : topData.topN_disk.indexOf(disk) == 1
                                              ? new Image.asset(
                                            "images/top2.png",
                                            width: 16.0,
                                          )
                                              : topData.topN_disk.indexOf(disk) == 2
                                              ? new Image.asset(
                                            "images/top3.png",
                                            width: 16.0,
                                          )
                                              : new SizedBox(
                                            width: 16.0,
                                          ),
                                          new SizedBox(
                                            width: 10.0,
                                          ),
                                          new Text(
                                            disk['phone'] != "" ? disk['phone'] : "邮箱用户",
                                            style: new TextStyle(
                                              fontSize: 16.0,
                                              color: new Color(0xFF666666),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    new Expanded(
                                      flex: 3,
                                      child: new Text(
                                        disk['disk'].toString() + " GB",
                                        style: new TextStyle(
                                          fontSize: 16.0,
                                          color: new Color(0xFF666666),
                                        ),
                                        textAlign: TextAlign.right,
                                      ),
                                    ),
                                    new Expanded(
                                      flex: 2,
                                      child: new Text(
                                        disk['wkb'].toStringAsFixed(decimal) + "个",
                                        style: new TextStyle(
                                          fontSize: 16.0,
                                          color: new Color(0xFF666666),
                                        ),
                                        textAlign: TextAlign.right,
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }).toList(),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                new Padding(
                  padding: new EdgeInsets.only(left: 10.0),
                  child: new Image.asset(
                    "images/toplist3.png",
                    width: 50.0,
                  ),
                ),
              ],
            ),
            new SizedBox(
              height: 10.0,
            ),
          ],
        ),
      ),
    );
  }
}
